//
//  ViewController.m
//  ForAddSDK
//
//  Created by admin on 2018/1/8.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "ViewController.h"
#import "DragDropImageView.h"
#import "DataHandler.h"

@interface ViewController()
@property (weak) IBOutlet NSPopUpButton *gameIDPopUpButton;
@property (weak) IBOutlet NSPathControl *svnPathControl;
@property (weak) IBOutlet NSTextField *projectSubPathTextField;
@property (weak) IBOutlet NSTextField *pictureSubPathTextField;
@property (weak) IBOutlet NSTextField *originPlatformTextField;
@property (weak) IBOutlet NSTextField *workPlatformTextField;
@property (unsafe_unretained) IBOutlet NSTextView *logTextView;

@end

@implementation ViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [_gameIDPopUpButton setAction:@selector(gameIDChange:)];
    [self setupGameVersionID];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setupGameVersionID) name:DefaultDataChangeNotification object:nil];
}

- (IBAction)gameIDChange:(id)sender {
    NSString *gameID = _gameIDPopUpButton.selectedItem.title;
    if (![gameID isEqualToString:@"none"]) {
        NSNotification *notification = [[NSNotification alloc] initWithName:SelectedGameIDChangeNotification object:gameID userInfo:nil];
        [[NSNotificationCenter defaultCenter] postNotification:notification];
        NSDictionary *dic = [DataHandler getDefaultValuesWithGameID:gameID];
        if (dic) {
            _svnPathControl.stringValue = dic[COLUMNID_LOCAL_SVN_PATH];
        }
    }
}

- (void)setupGameVersionID
{
    [_gameIDPopUpButton removeAllItems];
    [_gameIDPopUpButton addItemWithTitle:@"none"];
    NSArray *data = [DataHandler getDefaultValuesArray];
    for (NSDictionary *dic in data) {
        [_gameIDPopUpButton addItemWithTitle:[dic objectForKey:COLUMNID_GAME_ID]];
    }
}

- (IBAction)confirmButtonOnClick:(id)sender {
    NSLog(@"svnPath = %@",_svnPathControl.URL.path);
    NSLog(@"projectSubPath = %@", _projectSubPathTextField.stringValue);
    NSLog(@"pictureSubPath = %@", _pictureSubPathTextField.stringValue);
    NSLog(@"originPlatform = %@", _originPlatformTextField.stringValue);
    NSLog(@"workPlatform = %@", _workPlatformTextField.stringValue);
    if([self checkValue]) {
        [DataHandler shareInstance].isPathOK = YES;
        if (![DataHandler shareInstance].originPlatform.length) {
            _logTextView.string = @"信息已保存";
            return;
        }
        _logTextView.string = [DataHandler runScript:@"changeDirectoryName" type:@"sh" launchPath:@"/bin/bash" arguments:[[DataHandler shareInstance] getValueArray]];
    }
    else {
        [DataHandler shareInstance].isPathOK = NO;
    }
}

- (BOOL)checkValue {
    DataHandler *handler = [DataHandler shareInstance];
    handler.svnPath = DEL_SPACE(_svnPathControl.URL.path);
    handler.projectSubPath = DEL_SPACE(_projectSubPathTextField.stringValue);
    handler.pictureSubPath = DEL_SPACE(_pictureSubPathTextField.stringValue);
    handler.originPlatform = DEL_SPACE(_originPlatformTextField.stringValue);
    handler.workPlatform = DEL_SPACE(_workPlatformTextField.stringValue);
    if (![DataHandler exitPath:[NSString stringWithFormat:@"%@",handler.svnPath]]) {
        _logTextView.string = @"svn版本库路径不存在";
        return NO;
    }
    if(handler.svnPath.length && handler.workPlatform.length) {
        if (handler.projectSubPath.length == 0 && handler.pictureSubPath.length == 0) {
            _logTextView.string = @"版本哭工程的子路径和图片资源子路径至少填写一个";
            return NO;
        }
        else {
            if (handler.projectSubPath.length) {
                if (![DataHandler exitPath:[NSString stringWithFormat:@"%@%@",handler.svnPath,handler.projectSubPath]]) {
                    _logTextView.string = @"版本库中工程子路径不存在";
                    return NO;
                }
                else {
                    if (handler.originPlatform.length) {
                        if (![DataHandler isExistPlatformProject:handler.originPlatform]) {
                            _logTextView.string = [NSString stringWithFormat:@"%@工程不存在",handler.originPlatform];
                            return NO;
                        }
                    }
                    else {
                        if (![DataHandler isExistPlatformProject:handler.workPlatform]) {
                            _logTextView.string = [NSString stringWithFormat:@"%@工程不存在",handler.workPlatform];
                            return NO;
                        }
                    }
                }
            }
            if (handler.pictureSubPath.length) {
                if (![DataHandler exitPath:[NSString stringWithFormat:@"%@%@",handler.svnPath,handler.pictureSubPath]]) {
                    _logTextView.string = @"版本库中图片资源子路径不存在";
                    return NO;
                }
                else {
                    if (handler.originPlatform.length) {
                        if (![DataHandler isexistPictureFolder:handler.originPlatform]) {
                            _logTextView.string = [NSString stringWithFormat:@"%@图片资源不存在",handler.originPlatform];
                            return NO;
                        }
                    }
                    else {
                        if (![DataHandler isexistPictureFolder:handler.workPlatform]) {
                            _logTextView.string = [NSString stringWithFormat:@"%@图片资源不存在",handler.workPlatform];
                            return NO;
                        }
                    }
                }
            }
        }
        return YES;
    }
    else {
        _logTextView.string = @"svn版本库路径和要接入的平台名为必填项";
        return NO;
    }
}

- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}


@end
